package com.egov.config.jackson;

import com.egov.config.annotation.dic.Dic;
import com.egov.config.annotation.dic.DicSerializer;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;


import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created with IntelliJ IDEA.
 * Description: 实体类字典字段注解解析器
 *
 * @Author: denggang
 * @Date: 2018-09-11 9:05
 **/
public class AnnotationIntrospector extends JacksonAnnotationIntrospector {

    private static final long serialVersionUID = -4018475526828731883L;

    private static final Map<String, DicSerializer> cache = new ConcurrentHashMap<>();

    @Override
    public Object findSerializer(Annotated annotated) {
        if (annotated instanceof AnnotatedMethod) {
            AnnotatedMethod am = (AnnotatedMethod) annotated;
            Dic ann = am.getAnnotation(Dic.class);
            if (ann != null) {
//                String tableName = ann.tableName();
                String colName = ann.colName();
                String valueName = ann.valueName();
                String codeTypeValue = ann.codeTypeValue();
//                String codeType = ann.codeType();
//                String key = tableName + colName + valueName+codeTypeValue;
                String key = colName + valueName + codeTypeValue;
                DicSerializer serializer = cache.get(key);
                if (serializer == null) {
                    serializer = new DicSerializer(colName, valueName,codeTypeValue);
                    cache.put(key, serializer);
                }
                return serializer;
            }
        }
        return super.findSerializer(annotated);
    }


}
